package com.haha.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.haha.gulimall.product.entity.BrandEntity;
import com.haha.gulimall.product.vo.BrandVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.haha.gulimall.product.entity.CategoryBrandRelationEntity;
import com.haha.gulimall.product.service.CategoryBrandRelationService;
import com.haha.common.utils.PageUtils;
import com.haha.common.utils.R;


/**
 * 品牌分类关联
 *
 * @author xiongweizhong
 * @email xiongwei@gmail.com
 * @date 2022-06-22 23:36:06
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("product:categorybrandrelation:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }
    /**
     * /product/categorybrandrelation/brands/list
     * 获取三级分类的所有品牌
     */
    @GetMapping("/brands/list")
    public R getCategoryBrandRelation(@RequestParam(value = "catId",required = true) Long catId){
        System.out.println("+++++++++++"+catId);
        List<BrandEntity> brandEntities=categoryBrandRelationService.getBrandsByCatIds(catId);
        if(brandEntities!=null && brandEntities.size()!=0){
            List<BrandVo> BrandVo = brandEntities.stream().map(obj -> {
                if(obj==null) return null;
                BrandVo brandVo = new BrandVo();
                brandVo.setBrandId(obj.getBrandId());
                brandVo.setBrandName(obj.getName());
                return brandVo;
            }).collect(Collectors.toList());
            return R.ok().put("data", BrandVo);
        }
       return null;

    }

    /**
     * 获取品牌关联的分类
     */
    @RequestMapping("/catelog/list")
    public R list(@RequestParam Long brandId){
       List<CategoryBrandRelationEntity> data = categoryBrandRelationService.querylist(brandId);
        return R.ok().put("data", data);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("product:categorybrandrelation:info")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){

        categoryBrandRelationService.saveSimple(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("product:categorybrandrelation:update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("product:categorybrandrelation:delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
